package com.hibase.core.rule.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.log4j.Log4j2;

import java.util.Date;

/**
 * TODO
 *
 * @author EDZ
 * @version 1.0
 * @date 2020/10/28 14:50
 */
@Log4j2
public class DateUtils {

    /**
     * 通过时间戳比较大小
     * @param time1
     * @param time2
     * @return
     * 比较结果，如果date1 < date2，返回数小于0，date1==date2返回0，date1 > date2 大于0
     */
    public static int compare(long time1,long time2){
        Date date1 = new Date(time1);
        Date date2 = new Date(time2);
        return DateUtil.compare(date1,date2);
    }

    /**
     * 将字符串日期 转换为 时间戳
     * @param time
     * @param format
     * @return
     */
    public static long timestamp(String time,String format){
        DateTime dateTime = DateUtil.parse(time, format);
        return dateTime.getTime();
    }

    /**
     * 将字符串日期 转换为 时间戳
     * @param time
     * @return
     */
    public static long timestamp(String time){
        DateTime dateTime = DateUtil.parse(time, "yyyy-MM-dd HH:mm:ss");
        return dateTime.getTime();
    }

    /**
     * 将时间搓 转换为 字符串
     * @param time
     * @param format
     * @return
     */
    public static String timestamp(Long time,String format){
        Date date = new Date(time);
        return DateUtil.format(date,format);
    }

    /**
     * 通过已知时间 增加天数 增加时间 得到 时间
     */
    public static Date getTimestampByAddTime(Object obj, String addDay,String addTime){
        Date time = format(obj);
        DateTime addDayTime = DateUtil.offset(time, DateField.DAY_OF_MONTH, Integer.valueOf(addDay));
        String yyyyMMddTime = DateUtil.format(addDayTime, "yyyyMMdd");
        StringBuilder sb = new StringBuilder();
        sb.append(yyyyMMddTime)
                .append(addTime);
        return DateUtil.parse(sb.toString());
    }

    public static Date stringToDate(String str){
        return DateUtil.parse(str);
    }

    public static void main(String[] args) {
        //TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
//        Long timestampByAddTime = getTimestampByAddTime(1605334777777L, 1 + "", 160000 + "");
//        log.info(timestampByAddTime);
//        log.info(new Date(1605334777777L));
//        Date date = new Date();
//        log.info(date);
        Date date1 = stringToDate("2020-11-20 10:07:21.0");
        log.info(date1);
    }

    public static String format(Object obj,String format){
        Date time = format(obj);
        return DateUtil.format(time,format);
    }

    /**
     * object转日期
     * @param obj
     * @return
     */
    public static Date format(Object obj){
        Date time =null;
        // 字符串
        if(obj instanceof String){
            time = DateUtil.parse(obj.toString());
        }
        //时间戳
        if(obj instanceof Long){
            time = new Date(Long.parseLong(obj.toString()));
        }
        //日期
        if(obj instanceof Date){
            time = (Date) obj;
        }
        return time;
    }
}
